package com.wufeng.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wufeng.dto.ChatRecordDto;
import com.wufeng.dto.R;
import com.wufeng.dto.RichEvaluationDto;
import com.wufeng.entity.Evaluate;
import com.wufeng.entity.User;
import com.wufeng.service.EvaluateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/evaluate")
public class EvaluateController {

    @Autowired
    private EvaluateService evaluateService;


    /**
     * 根据类型获取测试列表
     *
     * @param type 测试类型
     * @return
     */
    @GetMapping("/getEvaluateListByType")
    public R getEvaluateListByType(@RequestParam("type") Integer type) {
        List<Evaluate> list = evaluateService.list(new QueryWrapper<Evaluate>().eq("type", type));

        HashMap<String, Object> map = new HashMap<>();
        map.put("list", list);

        return R.ok(map);
    }

    /**
     * 根据id获取测试信息
     *
     * @param evaluateId
     * @return
     */
    @GetMapping("/getEvaluateById")
    public R getEvaluateById(@RequestParam("evaluateId") Integer evaluateId) {
        Evaluate evaluate = evaluateService.getById(evaluateId);
        HashMap<String, Object> map = new HashMap<>();
        map.put("evaluate", evaluate);
        return R.ok(map);
    }


    /**
     * 插入evaluate，返回evaluateid
     *
     * @param evaluate
     * @return
     */
    @PostMapping("/insertEvaluate")
    public R insertEvaluate(@RequestBody Evaluate evaluate) {
        Integer evaluateid = evaluateService.saveEvaluate(evaluate);
        HashMap<String, Object> map = new HashMap<>();
        map.put("evaluateid", evaluateid);
        return R.ok(map);
    }


    /***
     * 插入（富）心理知识
     * @param richEvaluationDto
     * @return
     */
    @PostMapping("/insertRichEvaluate")
    public R insertRichEvaluate(@RequestBody RichEvaluationDto richEvaluationDto) {
        evaluateService.saveRichEvaluate(richEvaluationDto);
        return R.ok();
    }


    /**
     * 根据条件获取列表
     *
     * @param params
     * @return
     */
    @PostMapping("/getEvaluationByCondition")
    public R getEvaluationByCondition(@RequestBody Map<String, Object> params) {
        Integer pageIndex = (Integer) params.get("pageIndex");
        Integer pageSize = (Integer) params.get("pageSize");
        if (pageIndex == null || pageSize == null) {
            pageIndex = 1;
            pageSize = 5;
        }
        HashMap<String, Object> map = new HashMap<>();
        String title = (String) params.get("title");
        Integer type = (Integer) params.get("type");
        List<Evaluate> evaluateList = evaluateService.getEvaluationByCondition(title, type, pageIndex, pageSize);
        map.put("evaluateList", evaluateList);
        return R.ok(map);
    }

    @PostMapping("/countEvaluationByCondition")
    public R countEvaluationByCondition(@RequestBody Map<String, Object> params) {

        HashMap<String, Object> map = new HashMap<>();
        String title = (String) params.get("title");
        Integer type = (Integer) params.get("type");
        Integer count = evaluateService.countEvaluationByCondition(title, type);
        map.put("total", count);
        return R.ok(map);
    }


    @PostMapping("/updateEvaluationById")
    public R updateEvaluationById(@RequestBody Evaluate evaluate) {
        boolean b = evaluateService.updateById(evaluate);
        return b ? R.ok("修改成功！") : R.error("修改失败！");
    }
}
